package com.shanling.shanlingcontroller.view;

import android.content.Context;
import android.support.annotation.NonNull;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.amazon.alexa.voicechrome.VoiceChromeVisuals;
import com.amazon.alexa.voicechrome.internal.views.ListeningView;
import com.amazon.alexa.voicechrome.internal.views.SpeakingView;
import com.amazon.alexa.voicechrome.internal.views.ThinkingView;
import com.shanling.shanlingcontroller.R;
import com.shanling.shanlingcontroller.adapter.CommonAdapter;
import com.shanling.shanlingcontroller.adapter.ViewHolder;
import com.shanling.shanlingcontroller.bean.MybluetoothDevice;
import com.shanling.shanlingcontroller.utils.DpUtils;
import com.shanling.shanlingcontroller.utils.PreferenceUtil;

import java.util.List;

/**
 * Created by ch1 on 2019/11/4.
 */

public class AlexaStatePop extends PopupWindow {

    private Context context;
    private LayoutInflater inflater;
    private ThinkingView thinking_view;
    private ListeningView listening_view;
    private SpeakingView SpeakingView_view;

    public AlexaStatePop(Context context){
        this.context = context;
        inflater = LayoutInflater.from(context);
        initPopupWindow();
    }

    private void initPopupWindow() {
        View popupView = inflater.inflate(R.layout.statepop_layout, null);
        thinking_view=popupView.findViewById(R.id.thinking_view);
        listening_view=popupView.findViewById(R.id.listening_view);
        SpeakingView_view=popupView.findViewById(R.id.SpeakingView_view);
        this.setContentView(popupView);
        this.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
        this.setHeight(ViewGroup.LayoutParams.MATCH_PARENT);
        this.setFocusable(true);
        this.setTouchable(true);
        this.setOutsideTouchable(true);
        this.update();
    }

    public void show(View view){
        if (view != null){
            this.showAsDropDown(view);
        }
    }

    /**
     * Hide the VoiceChromeVisuals and cancel recording if active
     */
    public void stop() {
        if(isShowing()){
            thinking_view.stop();
            listening_view.stop();
            SpeakingView_view.stop();
        }
    }

    /**
     * Move the VoiceChromeVisuals to the listening state and begin listening to voice commands
     */
    public void listen() {
        if(isShowing()){
            listening_view.setVisibility(View.VISIBLE);
            listening_view.start();
            SpeakingView_view.setVisibility(View.GONE);
            thinking_view.setVisibility(View.GONE);
        }
    }

    /**
     * Move the VoiceChromeVisuals to the thinking state and stop recording. This should be called when
     * audio is being processed.
     */
    public void think() {
        if(isShowing()){
            thinking_view.setVisibility(View.VISIBLE);
            thinking_view.start();
            SpeakingView_view.setVisibility(View.GONE);
            listening_view.setVisibility(View.GONE);

        }
    }

    /**
     * Move the VoiceChromeVisuals to the speaking state and stop recording. This should be called when
     * audio has been processed and Alexa is responding.
     */
    public void speak() {
        if(isShowing()){
            SpeakingView_view.setVisibility(View.VISIBLE);
            SpeakingView_view.start();
            thinking_view.setVisibility(View.GONE);
            listening_view.setVisibility(View.GONE);

        }
    }

    public void updateAnimation(float v){
        if(isShowing()){
            listening_view.updateAnimation(v);
        }
    }
}
